package com.pucheng.bracelet.app.weight.recyclerview;

import android.graphics.Rect;
import android.view.View;

import androidx.core.text.TextUtilsCompat;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.RecyclerView;

public abstract class BaseCustomRtlDecoration extends RecyclerView.ItemDecoration {

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        getCustomItemOffsets(outRect, view, parent, state);
        if (TextUtilsCompat.getLayoutDirectionFromLocale(
                view.getContext().getResources().getConfiguration().locale) == ViewCompat.LAYOUT_DIRECTION_RTL){
            //使用异或操作完成数据交换，以免创建额外变量。
            outRect.right = outRect.left ^ outRect.right;
            outRect.left = outRect.left ^ outRect.right;
            outRect.right = outRect.left ^ outRect.right;
        }
    }

    //所有子ItemDecoration实现此方法，用法与getItemOffsets一致
    public void getCustomItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { }
}
